package com.bite.book.service;

import com.bite.book.enums.StatusEnum;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageResult;
import com.bite.book.model.RequestPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer addBook(BookInfo bookInfo) {
       return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        //1. count
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(), requestPage.getPageSize());

        for (BookInfo bookInfo: bookInfos){
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<>(bookInfos, count, requestPage);

    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
